package cn.ivicar.message.push.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.model.PushCustomerClient;
import cn.com.carsmart.ws.service.PushCustomerClientManager;

public class NotificationService {

	private static transient final Logger LOGGER = LoggerFactory.getLogger(NotificationService.class);

	@Inject
	private PushCustomerClientManager customerClientManager;
    /*
    注册极光推送
     */
	public void register(Long appId, Long customerId, String clientId, Integer clientType) throws CarsmartException {
		PushCustomerClient params = new PushCustomerClient();
		params.setAppId(appId);
		params.setCustomerId(customerId);
		params.setClientId(clientId);
		params.setClientType(clientType);
		List<PushCustomerClient> list = customerClientManager.listByExample(params);
		if (list == null || list.isEmpty()) {
			params.setStatus(1);
			customerClientManager.insert(params);
		} else {
			PushCustomerClient client = list.get(0);
			client.setStatus(1);
			customerClientManager.update(client);
		}
	}
    /*
    登出极光推送
     */
	public void logout(Long appId, Long customerId, String clientId, Integer clientType) throws CarsmartException {
		PushCustomerClient params = new PushCustomerClient();
		params.setAppId(appId);
		params.setCustomerId(customerId);
		params.setClientId(clientId);
		params.setClientType(clientType);
		List<PushCustomerClient> list = customerClientManager.listByExample(params);
		if (list == null || list.isEmpty()) {
			params.setStatus(0);
			customerClientManager.insert(params);
		} else {
			PushCustomerClient client = list.get(0);
			client.setStatus(0);
			customerClientManager.update(client);
		}
	}
    /*
    获取pushcustomerclient实体类为样式的列表
     */
	public List<PushCustomerClient> get(Long customerId, Integer status) {
		PushCustomerClient params = new PushCustomerClient();
		params.setCustomerId(customerId);
		if (status != null) {
			params.setStatus(status);
		}
		List<PushCustomerClient> list = customerClientManager.listByExample(params);
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list;
	}
	
	/**
	 * 获取可用的clientId列表
	 */
	public List<PushCustomerClient> get(Long customerId) {
		return this.get(customerId, 1);
	}

}
